package com.cardbaobao.cardbabyclient.controller;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.cardbaobao.cardbabyclient.BuildConfig;
import com.cardbaobao.cardbabyclient.ui.activity.SpeedCardActivity;
import com.cardbaobao.cardbabyclient.ui.activity.SpeedLoanActivity;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.activity.web.AgreementWebViewActivity;
import com.cardbaobao.cardbabyclient.ui.activity.web.ChannelWebViewActivity;
import com.cardbaobao.cardbabyclient.base.App;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.ui.fragment.applyrecord.CreditApplyRecordFragment;
import com.cardbaobao.cardbabyclient.ui.fragment.applyrecord.LoansApplyRecordFragment;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.utils.PhoneInfoUtil;
import com.cardbaobao.cardbabyclient.utils.SharedPreferencesUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.net.URLEncoder;

/**
 * Created by Administrator on 2017/1/11.
 */

public class Controller {
    private Context mContext;
    private App mApplication;

    public Controller(Context context) {
        mContext = context;
        mApplication = ApplicationHelper.getApplication();
    }

    /**
     * 设置此次引导页的已展示
     */
    public void setFirstStart() {
        SharedPreferencesUtil.CbbInfo.setAppBeUsed();
    }

    /**
     * 当前版本有没有启动过引导页
     *
     * @return 获取SharedPreferences中first_start的值是否为true
     */
    public boolean getFirstStart() {
        return SharedPreferencesUtil.CbbInfo.isAppBeUsed();
    }

    public String getVersion() {//获取版本号
        try {
            PackageInfo pi = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
            return pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "未知版本";
        }
    }

    /**
     * 贷款详情页面 申请贷款
     *
     * @param maxAmount        贷款额度
     * @param productLoansId   贷款产品id
     * @param productLoansName 贷款产品名称
     * @param page             进入到贷款详情的页面
     */
    public void detailPageApplyLoans(String maxAmount, String productLoansId, String productLoansName, String page) {
        Intent intent = new Intent();
        intent.setClass(mContext, ChannelWebViewActivity.class);
        String url = URLUtil.APPLY_CARD_LOAN + "?Site=APP&MaxAmount=" + maxAmount +
                "&ProductLoansID=" + productLoansId + "&MemberID=" +
                mApplication.getUser().getID()+
                "&sourceName=android_"+ PhoneInfoUtil.getApkChannelName(mContext);
        intent.putExtra(ChannelWebViewActivity.WEB_URL_S, url);
        String webTitle="卡借贷";
        //判断下，避免闪退
        if(!TextUtils.isEmpty(productLoansId)){
            if(Constant.PRODUCT_ENVIRONMENT.equals(BuildConfig.BUILD_TYPE)){
                //线上环境
                if(Constant.LKL_PRODUCT_ID.equals(productLoansId)){
                    webTitle=productLoansName;
                }
            }else if(SharedPreferencesUtil.ConfigInfo.getApiAddressType()==Constant.GRAY_ENVIRONMENT){
                //灰度环境
                if(Constant.LKL_GRAY_ID.equals(productLoansId)){
                    webTitle=productLoansName;
                }
            }
        }
        intent.putExtra(ChannelWebViewActivity.WEB_TITLE_S, webTitle);
        intent.putExtra(ChannelWebViewActivity.WEB_TYPE_I, 1);
        mContext.startActivity(intent);
    }

    /**
     * 信用卡详情页面 申请信用卡
     *
     * @param cardId   信用卡id
     * @param cardName 信用卡名称
     * @param fromPage 进入到信用卡详情的页面
     */
    public void detailPageApplyCreditCard(String cardId, String cardName,
                                          String cityId, String cityName,
                                          String fromPage) {
        Intent intent = new Intent();
        intent.setClass(mContext, ChannelWebViewActivity.class);
        try {
            //拼接url
            String url = URLUtil.APPLY_SINGLE_CREDIT +
                    "?id=" + cardId +
                    "&name=" + URLEncoder.encode(cardName, Constant.ENCODING) +
                    "&memberID=" + mApplication.getUser().getID() +
                    "&cityID=" + cityId +
                    "&cityName=" + URLEncoder.encode(cityName, Constant.ENCODING)+
                    "&sourceName=android_"+ PhoneInfoUtil.getApkChannelName(mContext);
            intent.putExtra(ChannelWebViewActivity.WEB_URL_S, url);
            intent.putExtra(ChannelWebViewActivity.WEB_TITLE_S, "卡办理");
            intent.putExtra(ChannelWebViewActivity.WEB_TYPE_I, 0);
            mContext.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 去常见问题页面
     */
    public void toCommonQuestion(Activity activity,String currentPage){
        Intent intent=new Intent(activity, AgreementWebViewActivity.class);
        intent.putExtra(AgreementWebViewActivity.WEB_TITLE_S,"常见问题");
        intent.putExtra(AgreementWebViewActivity.WEB_URL_S,
                URLUtil.COMMON_QUESTION);
        intent.putExtra(AgreementWebViewActivity.WEB_TYPE_I,11);
        intent.putExtra(BaseActivity.PRE_PAGE_ID_S,currentPage);
        activity.startActivity(intent);
    }
}
